package com.captainbank.tcl.tclapplication.ui.activity.function;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.EditText;
import android.widget.TextView;

import com.captainbank.tcl.tclapplication.R;
import com.captainbank.tcl.tclapplication.model.LzyResponse;
import com.captainbank.tcl.tclapplication.model.TestBean;
import com.captainbank.tcl.tclapplication.model.User;
import com.captainbank.tcl.tclapplication.ui.activity.basic.BaseActivity;
import com.captainbank.tcl.tclapplication.utils.ApiService;
import com.captainbank.tcl.tclapplication.utils.Constants;
import com.captainbank.tcl.tclapplication.utils.MyToast;
import com.captainbank.tcl.tclapplication.utils.NetTools;
import com.captainbank.tcl.tclapplication.utils.StringUtil;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import cn.magicwindow.MLinkAPIFactory;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by TCL on 2019/4/18.
 * Describe:
 */
public class Retrofit2Activity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_psw)
    EditText etPsw;
    @BindView(R.id.tv_show)
    TextView tvShow;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_retrofit2;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarColor(R.color.colorPrimary)
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    @Override
    protected void initView() {
        toolbar.setTitle(getString(R.string.title_retrofit2));
    }

    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        String name = etName.getText().toString().trim();
        String psw = etPsw.getText().toString().trim();
        if (!StringUtil.checkIsNotNull(name)) {
            MyToast.toastError(Retrofit2Activity.this, getString(R.string.hint_name), false);
        } else if (!StringUtil.checkIsNotNull(psw)) {
            MyToast.toastError(Retrofit2Activity.this, getString(R.string.hint_psw), false);
        } else {
            //处理参数
            HashMap<String, Object> map = new HashMap<>();
            map.put("mobile", name);
            map.put("password", psw);
//            String ciphertext = NetTools.getEncryptionString(map);

            //获取Retrofit对象
            Retrofit retrofit = new Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(Constants.URL_PRE)
                    .build();
            //然后通过动态代理获取到所定义的接口，获取到返回
            ApiService apiService = retrofit.create(ApiService.class);
            Call<LzyResponse<User>> repos = apiService.login(NetTools.getEncryptionRequestBody(map));
            repos.enqueue(new Callback<LzyResponse<User>>() {
                @Override
                public void onResponse(Call<LzyResponse<User>> call, Response<LzyResponse<User>> response) {
                    LzyResponse<User> lzyResponse = response.body();
                    System.out.println("输出lzyResponse：" + new Gson().toJson(lzyResponse));

                    User user = lzyResponse.result;
                    tvShow.setText("用户信息：\n" + new Gson().toJson(user));

                    //登录后场景还原（如果需要场景还原的话）
                    MLinkAPIFactory.createAPI(Retrofit2Activity.this).deferredRouter();
                }

                @Override
                public void onFailure(Call<LzyResponse<User>> call, Throwable t) {
                }
            });
        }
    }

}
